//
//  NSArray+XXExtension.h
//  SHWheel
//
//  Created by YYKJ on 2020/11/4.
//  Copyright © 2020 YYKJ. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray<ObjectType> (XXExtension)

/**
 Creates and returns an array from a specified property list data.
 
 @param plist   A property list data whose root object is an array.
 @return A new array created from the plist data, or nil if an error occurs.
 */
+ (nullable NSArray *)arrayWithPlistData:(NSData *)plist;

/**
 Creates and returns an array from a specified property list xml string.
 
 @param plist   A property list xml string whose root object is an array.
 @return A new array created from the plist string, or nil if an error occurs.
 */
+ (nullable NSArray *)arrayWithPlistString:(NSString *)plist;

/**
 Serialize the array to a binary property list data.
 
 @return A bplist data, or nil if an error occurs.
 */
- (nullable NSData *)plistData;

/**
 Serialize the array to a xml property list string.
 
 @return A plist xml string, or nil if an error occurs.
 */
- (nullable NSString *)plistString;

/**
 Returns the object located at a random index.
 
 @return The object in the array with a random index value.
 If the array is empty, returns nil.
 */
- (nullable id)randomObject;

/**
 Returns the object located at index, or return nil when out of bounds.
 It's similar to `objectAtIndex:`, but it never throw exception.
 
 @param index The object located at index.
 */
- (nullable id)objectOrNilAtIndex:(NSUInteger)index;

/**
 Convert object to json string. return nil if an error occurs.
 NSString/NSNumber/NSDictionary/NSArray
 */
- (nullable NSString *)jsonStringEncoded;

/**
 Convert object to json string formatted. return nil if an error occurs.
 */
- (nullable NSString *)jsonPrettyStringEncoded;


/// 过滤元素
/// @param block <#block description#>
- (NSArray<ObjectType> *)xx_filterWithBlock:(BOOL (NS_NOESCAPE^)(ObjectType item))block;

/// 转换数组元素
/// @param block <#block description#>
- (NSArray<ObjectType> *)xx_mapWithBlock:(id (NS_NOESCAPE^)(ObjectType item))block;

@end



@interface NSMutableArray (XXExtension)

/// Reverse the index of object in this array.
- (void)xx_reverse;


/// Sort the object in this array randomly.
- (void)xx_shuffle;
@end

NS_ASSUME_NONNULL_END
